package com.shijunhao.graduation.service.system.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.dto.ModifyPassDto;
import com.shijunhao.graduation.domain.entity.*;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.OrgUserMapper;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.service.system.IOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-03-02
 */
@Service
public class OrgUserServiceImpl extends BaseServiceImpl<OrgUserMapper, OrgUser> implements IOrgUserService {

    private final IBusinessUserInfoService businessUserInfoService;
    private final PasswordEncoder passwordEncoder;


    @Autowired
    public OrgUserServiceImpl(IBusinessUserInfoService businessUserInfoService, PasswordEncoder passwordEncoder) {
        this.businessUserInfoService = businessUserInfoService;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public OrgUser loadUserByName(String username) {
        return this.getOne(Wrappers.lambdaQuery(OrgUser.class).eq(OrgUser::getUsername,username));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long registry(OrgUser user) {
        List<OrgUser> orgUsers = this.selectByCondition(Wrappers.lambdaQuery(OrgUser.class).eq(OrgUser::getUsername, user.getUsername()));
        if(orgUsers.size() != 0)
            throw new BusinessException("用户名已存在");
        user.setRole(user.getRole());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setEnable(user.getEnable());
        super.insertOne(user);

        //添加用户信息
        BusinessUserInfo businessUserInfo = new BusinessUserInfo();
        businessUserInfo.setUid(user.getId());
        businessUserInfo.setCreateBy(user.getId());
        businessUserInfo.setUpdateBy(user.getId());
        Date date = new Date();
        businessUserInfo.setCreateDate(date);
        businessUserInfo.setUpdateDate(date);
        businessUserInfo.setAvatar(1L);
        businessUserInfo.setName("USER-"+IdUtil.simpleUUID());
        businessUserInfo.setGender(-1);
        businessUserInfoService.insertOne(businessUserInfo);
        return user.getId();
    }

    @Override
    public boolean insertBatch(Collection<OrgUser> users) {
        for(OrgUser user:users)
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        return super.insertBatch(users);
    }

    @Override
    public boolean insertOne(OrgUser user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return super.insertOne(user);
    }



    @Override
    public OrgUser getCurrentUser(Long uid) {
        return getById(uid);
    }

    @Override
    public boolean updatePass(Long uid,ModifyPassDto dto) {
        //检查旧密码是否符合规范
        String s = checkPass(dto.getOldPass());
        if(s != null)
            throw new BusinessException(s);
        //检查旧密码是否正确
        OrgUser user = getById(uid);
        if(!passwordEncoder.matches(dto.getOldPass(),user.getPassword()))
            throw new BusinessException("旧密码不正确");
        //检查新密码是否符合规范
        String s1 = checkPass(dto.getNewPass());
        if(s1 != null)
            throw new BusinessException(s1);
        String s2 = checkPass(dto.getCheckPass());
        if(s2 != null)
            throw new BusinessException(s2);
        //检查两次密码是否正确
        if(!dto.getNewPass().equals(dto.getCheckPass()))
            throw new BusinessException("两次密码不一致");
        //更新密码
        user.setPassword(passwordEncoder.encode(dto.getNewPass()));
        return updateById(user);
    }

    public String checkPass(String pass){
        if(pass == null || pass.length() == 0)
            return "密码信息不能为空";
        if(pass.length() < 6 || pass.length() > 18)
            return "密码长度必须在6-18之间";
        return null;
    }
}
